package com.kczd.jinlan.view.shopcar;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kczd.jinlan.R;
import com.kczd.jinlan.adapter.ShoppingCartAdapter;
import com.kczd.jinlan.db.shopcar.DbUtils;
import com.kczd.jinlan.model.ShoppingCartBean;
import com.kczd.jinlan.view.ConfirmOrderActivity;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * yang_g
 * 购物
 * Created by Administrator on 2017/5/17.
 */

public class ShoppingActivity extends BaseActivity implements View.OnClickListener, CheckInterface,ModifyCountInterface {
    private static final String TAG="ShoppingActivity.class";
    public TextView tv_title, tv_settlement, tv_show_price,tv_show_count,tv_edit;
    private ListView list_shopping_cart;
    private CheckBox ck_all;

    private ShoppingCartAdapter shoppingCartAdapter;


    private boolean flag = false;
    private double totalPrice = 0.00;// 购买的商品总价
    private int totalCount = 0;// 购买的商品总数量

    private List<ShoppingCartBean> shoppingCartBeanList = new ArrayList<>();
    public static List<ShoppingCartBean> alreadyShoplist=new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.home_fragment;
    }

    @Override
    protected void initView() {
        tv_title = bindView(R.id.tv_title);
        tv_title.setText("购物车");
        list_shopping_cart = bindView(R.id.list_shopping_cart);
        ck_all = bindView(R.id.ck_all);
        ck_all.setOnClickListener(this);
        tv_show_price = bindView(R.id.tv_show_price);
        tv_show_count=bindView(R.id.tv_show_count);
        tv_settlement = bindView(R.id.tv_settlement);
        tv_settlement.setOnClickListener(this);
        tv_edit = bindView(R.id.tv_edit);
        tv_edit.setOnClickListener(this);
        tv_edit.setCompoundDrawablePadding(6);//修改drawable与文字的间距
        shoppingCartAdapter = new ShoppingCartAdapter(this);
        shoppingCartAdapter.setCheckInterface(this);
        shoppingCartAdapter.setModifyCountInterface(this);
        list_shopping_cart.setAdapter(shoppingCartAdapter);
        shoppingCartAdapter.setShoppingCartBeanList(shoppingCartBeanList);
    }

    /**
     * 获取数据库的所有购物车信息
     */
    @Override
    protected void initData() {
        List<ShoppingCartBean> list= DataSupport.findAll(ShoppingCartBean.class);
        for(ShoppingCartBean sc:list)
        {
            shoppingCartBeanList.add(sc);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //全选按钮
            case R.id.ck_all:
                if (shoppingCartBeanList.size() != 0) {
                    if (ck_all.isChecked()) {
                        for (int i = 0; i < shoppingCartBeanList.size(); i++) {
                            shoppingCartBeanList.get(i).setChoosed(true);
                        }
                        shoppingCartAdapter.notifyDataSetChanged();
                    } else {
                        for (int i = 0; i < shoppingCartBeanList.size(); i++) {
                            shoppingCartBeanList.get(i).setChoosed(false);
                        }
                        shoppingCartAdapter.notifyDataSetChanged();
                    }
                }
                statistics();
                break;
            case R.id.tv_edit:
                flag = !flag;
                if (flag) {
                    tv_edit.setText("完成");
                    shoppingCartAdapter.isShow(false);
                } else {
                    tv_edit.setText("编辑");
                    shoppingCartAdapter.isShow(true);
                }
                break;
            case R.id.tv_settlement:

                ShoppingCartBean shoppingCartBean=null;
                for (int i = 0; i < shoppingCartBeanList.size(); i++) {
                    shoppingCartBean = shoppingCartBeanList.get(i);
                    if (shoppingCartBean.isChoosed()) {
                        alreadyShoplist.add(shoppingCartBean);
                    }
                }
                if(alreadyShoplist.size()!=0) {
                    Intent intent = new Intent(ShoppingActivity.this, ConfirmOrderActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(this,"请选择商品",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 单选
     *
     * @param position  组元素位置
     * @param isChecked 组元素选中与否
     */
    @Override
    public void checkGroup(int position, boolean isChecked) {

        shoppingCartBeanList.get(position).setChoosed(isChecked);

        if (isAllCheck())
            ck_all.setChecked(true);
        else
            ck_all.setChecked(false);

        shoppingCartAdapter.notifyDataSetChanged();
        statistics();
    }

    /**
     * 遍历list集合
     *
     * @return
     */
    private boolean isAllCheck() {

        for (ShoppingCartBean group : shoppingCartBeanList) {
            if (!group.isChoosed())
                return false;
        }
        return true;
    }

    /**
     * 统计操作
     * 1.先清空全局计数器<br>
     * 2.遍历所有子元素，只要是被选中状态的，就进行相关的计算操作
     * 3.给底部的textView进行数据填充
     */
    public void statistics() {
        totalCount = 0;
        totalPrice = 0.00;
        ShoppingCartBean shoppingCartBean=null;
        for (int i = 0; i < shoppingCartBeanList.size(); i++) {
            shoppingCartBean = shoppingCartBeanList.get(i);
            if (shoppingCartBean.isChoosed()) {
                totalCount++;
                totalPrice += Double.parseDouble(shoppingCartBean.getPrice()) * shoppingCartBean.getCount();
            }
        }
        tv_show_price.setText("合计:" + totalPrice);
        tv_show_count.setText("共" + totalCount+ "个");
    }

    /**
     * 增加
     *
     * @param position      组元素位置
     * @param showCountView 用于展示变化后数量的View
     * @param isChecked     子元素选中与否
     */
    @Override
    public void doIncrease(int position, View showCountView, boolean isChecked) {
        ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
        int currentCount = shoppingCartBean.getCount();
        currentCount++;
        shoppingCartBean.setCount(currentCount);
        ((TextView) showCountView).setText(currentCount + "");
        shoppingCartAdapter.notifyDataSetChanged();
        statistics();
    }

    /**
     * 删减
     *
     * @param position      组元素位置
     * @param showCountView 用于展示变化后数量的View
     * @param isChecked     子元素选中与否
     */
    @Override
    public void doDecrease(int position, View showCountView, boolean isChecked) {
        ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
        DbUtils.deleteData(shoppingCartBean.getId());
        int currentCount = shoppingCartBean.getCount();
        if (currentCount == 1) {
            return;
        }
        currentCount--;
        shoppingCartBean.setCount(currentCount);
        ((TextView) showCountView).setText(currentCount + "");
        shoppingCartAdapter.notifyDataSetChanged();
        statistics();

    }

    /**
     * 删除
     *
     * @param position
     */
    @Override
    public void childDelete(int position) {
        ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
        DbUtils.deleteData(shoppingCartBean.getId());
        shoppingCartBeanList.remove(position);
        shoppingCartAdapter.notifyDataSetChanged();
        statistics();
    }


}
